﻿using System.Collections.Generic;
namespace MTBase
{
    public interface Pool
    {
        void Push(PoolObject obj);
        PoolObject Pop();
    }

    public interface PoolObject
    {
        void OnPush();
    }
    public class QueuePool: Pool
    {
        public delegate PoolObject CreateFunc();
        private Queue<PoolObject> pool = new Queue<PoolObject>();
        private CreateFunc createfunc;

        public QueuePool(CreateFunc createFunc)
        {
            this.createfunc = createFunc;
        }
        public void Push(PoolObject obj)
        {
            obj.OnPush();
            pool.Enqueue(obj);
        }

        public PoolObject Pop()
        {
            PoolObject ret;
            if (pool.Count == 0)
            {
                ret = createfunc();
            }
            else
            {
                ret = pool.Dequeue();
            }

            return ret;
        }
    }
}
